<?php
	function get_course_category($course) {
		global $CFG;
		$sql = "SELECT category FROM {$CFG->prefix}course WHERE id = {$course}";
		$category = get_record_sql($sql);
		return $category->category;
	}
	
	function get_cat_name($cat) {
		global $CFG;
		$sql = "SELECT name FROM {$CFG->prefix}course_categories WHERE id = {$cat}";
		$category = get_record_sql($sql);
		return $category->name;
	}
	
	function get_etu_names($userid) {
		global $CFG;
		$sql = "SELECT firstname, lastname FROM {$CFG->prefix}user WHERE id = {$userid}";
		$user = get_record_sql($sql);
		return $user;
	}
	
	function getMinSec($secondes){
		$time[0]=floor($secondes/3600);//heures
		$reste=$secondes%3600;
		$time[1]=floor($reste/60);//minutes
		$time[2]=$reste%60;//secondes
		return $time;
	}
	
	//Rempli le tableau qu'on lui fourni
	function get_cat_student_cascade($categoryid, $etudiants){
		global $CFG;
		$sql = "SELECT u.id, u.lastname, u.firstname FROM {$CFG->prefix}user as u WHERE id IN (SELECT DISTINCT userid FROM {$CFG->prefix}role_assignments WHERE contextid IN (SELECT id as contextid FROM {$CFG->prefix}context WHERE instanceid IN (SELECT id as courseid FROM {$CFG->prefix}course ";
		if(!$categoryid == 0){
			$sql .= ' WHERE category = '.$categoryid;
		}
		$sql .= ') AND contextlevel = 50)) ';
		$students = get_records_sql($sql);//On r�cup�re la liste des etudiants correspondants.
		$etudiants[0] = get_string('alletu', 'block_connexion_tracking');
		foreach ($students as $student) {
			$etudiants[$student->id] = $student->firstname." ".$student->lastname ;
		}
		$subcategories = get_records_select("course_categories", "parent = {$categoryid}", "");
		foreach($subcategories as $subcategorie){
			get_cat_student_cascade($subcategorie->id, &$etudiants);
		}
	}
	
	//Rempli l'objet DOM qu'on lui fourni avec la liste des utilisateurs d'une cat�gorie.
	function get_cat_all_cascadeXML($categoryid, $dom, $portee){

		global $CFG;
		$sql = "SELECT u.id, u.lastname, u.firstname FROM {$CFG->prefix}user as u WHERE id IN (SELECT DISTINCT userid FROM {$CFG->prefix}role_assignments WHERE contextid IN (SELECT id as contextid FROM {$CFG->prefix}context WHERE instanceid IN (SELECT id as courseid FROM {$CFG->prefix}course ";
		if(!$categoryid == 0){
			$sql .= ' WHERE category = '.$categoryid;
		}
		$sql .= ') AND contextlevel = 50) '.$portee.') ';
		$students = get_records_sql($sql);//On r�cup�re la liste des etudiants correspondants.
		//$etudiants[0] = get_string('alletu', 'block_connexion_tracking');
		$messageList = $dom->getElementsByTagName('message');
		$message = $messageList->item(0);
		foreach ($students as $student) {
			$etudiant = $dom->createElement('student');
			$etudiant = $message->appendChild($etudiant);//On ins�re l'�tuduant
			//On rempli ses attributs
			$etudiant->setAttribute('firstname', $student->firstname);
 			$etudiant->setAttribute('lastname', $student->lastname);
			$etudiant->setAttribute('id', $student->id);
		}
		$subcategories = get_records_select("course_categories", "parent = {$categoryid}", "");
		foreach($subcategories as $subcategorie){
			get_cat_all_cascadeXML($subcategorie->id, &$dom);
		}
	}
	
	function deleteDuplicatesXML($dom){
		//Pour chaque �l�ment du dom on cherche si un id utilisateur est dupliqu�, auquel cas on supprime les enfants concern�s.
		//Attention les yeux, on applique un feuille de syle XSLT
		$xsl = new DOMDocument;
		$xsl->load("http://localhost:8888/Arpe-deployed/blocks/connexion_tracking/removeDuplicates.xsl");

		// Transformation !
		$proc = new XSLTProcessor;
		$proc->importStyleSheet($xsl);
		$dom = $proc->transformToDoc($dom); 
	}
	
	function print_sub_cat($cat, $categoriesArray){//Categorie mere et le tableau ou ecrire le couple [id, desc]
		global $CFG;
		//On r�cup�re la profondeur de la cat�gorie courante.
		$tableau = explode("/", $cat->path);
		$depth = count($tableau) - 2;//0 = racine, 1 = niveau 1...
		//Ecriture
		$i = 0;
		while($i <= $depth){
			$output .= ($i == $depth ) ? "+" : "&nbsp;&nbsp;";
			$i++;
		}
		$categoriesArray[$cat->id] = $output.$cat->name;
		//On traite les ss-categories.
		$subcategories = get_records_select("course_categories", "parent = {$cat->id}", "");
		foreach($subcategories as $subcategorie){
			if(!array_search($subcategorie->id, $ttt)){//Si on a pas d�j� affich�e cette cat�gorie	
				print_sub_cat($subcategorie, &$categoriesArray);
			}
		}
	}
	
	function get_cascade_cat($catid){//Renvoie un tableau contenant la liste des categories filles.
		static $subcats = array();
		$subcategories = get_records_select("course_categories", "parent = {$catid}", "");
		foreach($subcategories as $subcategorie){
			$subcats[$subcategorie->id] = $subcategorie->id;//Pour eviter les doublons.
			get_cascade_cat($subcategorie->id);
		}
		return $subcats;
	}
	
	function filter($in) {
		$search = array ('@[������]@i','@[�����]@i','@[����]@i','@[�����]@i','@[����]@i','@[�]@i','@[ ]@i','@[^a-zA-Z0-9_]@');
		$replace = array ('e','a','i','u','o','c','-','');
		return preg_replace($search, $replace, $in);
	}
	
?>